﻿using MiniExcelLibs;
using MiniExcelLibs.Attributes;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
 

namespace ConsoleApp12
{
    internal class Program
    {




        public static string CleanText(string input)
        {
            string pattern = @"\。\s*";
            return Regex.Replace(input, pattern, "。");
        }

        public static byte[] ConvertToThumbnail(string imagePath, int compression = 100, int thumbWidth = 0, int thumbHeight = 0)
        {
            byte[] bs = null;
            try
            {
                using (Image originalImage = Image.FromFile(imagePath))
                {

                    if (thumbWidth == 0 && thumbHeight == 0)
                    {
                        thumbWidth = originalImage.Width;
                        thumbHeight = originalImage.Height;
                    }
                    using (Bitmap dstimg = new Bitmap(thumbWidth, thumbHeight))//图片压缩质量
                    {
                        //从Bitmap创建一个System.Drawing.Graphics对象，用来绘制高质量的缩小图。
                        using (Graphics gr = Graphics.FromImage(dstimg))
                        {
                            //把原始图像绘制成上面所设置宽高的缩小图
                            System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
                            gr.Clear(Color.WhiteSmoke);
                            gr.CompositingQuality = CompositingQuality.HighQuality;
                            gr.SmoothingMode = SmoothingMode.HighQuality;
                            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            gr.DrawImage(originalImage, rectDestination, 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);

                            EncoderParameters ep = new EncoderParameters(1);
                            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);//设置压缩的比例1-100
                            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                            ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(t => t.FormatID == System.Drawing.Imaging.ImageFormat.Png.Guid);

                            using (MemoryStream dstms = new MemoryStream())
                            {
                                if (jpegICIinfo != null)
                                {
                                    dstimg.Save(dstms, jpegICIinfo, ep);
                                }
                                else
                                {
                                    dstimg.Save(dstms, System.Drawing.Imaging.ImageFormat.Png);//保存到内存里
                                }


                                bs = new Byte[dstms.Length];
                                dstms.Position = 0;
                                dstms.Read(bs, 0, bs.Length);
                                //countss++;
                                //SaveImage(bs,$@"c:\out{countss}.png");
                            }

                            //return Convert.ToBase64String(bs);
                            return bs;

                        }
                    }
                }
            }
            catch { }
            return bs;
        }

        public static bool DateRangeIsRight(DateTime startA, DateTime endA, DateTime startB, DateTime endB)
        {
            var dateAs = Enumerable.Range(0, (endA - startA).Days + 1).Select(d => startA.Date.AddDays(d)).ToList();
            var dateBs = Enumerable.Range(0, (endB - startB).Days + 1).Select(d => startB.Date.AddDays(d)).ToList();
            return dateAs.Any(a => dateBs.Contains(a));
        }
        static void Main(string[] args)
        {
            // 创建一个 200x200 像素的透明位图
            int width = 200;
            int height = 200;

            using (Bitmap bitmap = new Bitmap(width, height))
            {
                // 设置图像的背景为透明
                bitmap.MakeTransparent();

                // 使用 Graphics 对象在位图上绘制内容
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // 设置背景色为透明
                    g.Clear(Color.Transparent);

                    // 绘制一个红色矩形
                    using (Brush brush = new SolidBrush(Color.Red))
                    {
                        g.FillRectangle(brush, 50, 50, 100, 100);
                    }
                }

                // 保存位图为 PNG 文件
                bitmap.Save("transparent_image.png", System.Drawing.Imaging.ImageFormat.Png);
            }

            Console.WriteLine("透明位图已生成。");



            Training training = new Training();

            var a = training.TrainingType;





            DateTime startA = new DateTime(2024, 11, 8, 10, 0, 0); // 开始A
            DateTime endA = new DateTime(2024, 11, 9, 12, 0, 0);   // 结束A
            DateTime startB = new DateTime(2024, 11, 7, 12, 30, 0); // 开始B
            DateTime endB = new DateTime(2024, 11, 19, 14, 0, 0);    // 结束B
            Console.WriteLine(DateRangeIsRight(startA, endA, startB, endB));





            string sss222 = $"{System.AppDomain.CurrentDomain.BaseDirectory}ffmpeg.exe";

            List<QuestionOutDataV1> questionImportDatas = new List<QuestionOutDataV1>();
            List<QuestionInput> questionInputs1 = new List<QuestionInput>();
            string folderPath = @"C:\Users\humengjian\Desktop\新建文件夹 (3)";

            // 获取指定文件夹下的所有文件
            string[] files = Directory.GetFiles(folderPath);

            // 遍历并输出每个文件的路径
            foreach (string file in files)
            {
                string fileName = Path.GetFileNameWithoutExtension(file);

                using (Stream stream = File.OpenRead(file))
                {
                    List<string> list = new List<string>();
                    var questionInputs = stream.Query<QuestionImportData>().ToList();

                    for (int i = 0; i < questionInputs.Count; i++)
                    {
                        try
                        {
                            int questionType = 0;
                            switch (questionInputs[i].QuestionType)
                            {
                                case "1"://单选
                                    questionType = 2;
                                    break;
                                case "2":
                                    questionType = 3;
                                    break;
                                case "3"://判断
                                    questionType = 1;
                                    break;
                                default:
                                    break;
                            }
                            string options = string.Empty;
                            AnswerAndAnalysisEntity answerAndAnalysisEntity = JsonConvert.DeserializeObject<AnswerAndAnalysisEntity>(questionInputs[i].AnswerAndExplain);
                            string explain = answerAndAnalysisEntity.analysis;
                            string answer = String.Empty;
                            switch (questionType)
                            {
                                case 1://判断
                                    List<WxOptionsInfo> wxOptions = new List<WxOptionsInfo>();
                                    WxOptionsInfo wxOptionsInfo = new WxOptionsInfo()
                                    {
                                        OptionTitle = "A",
                                        OptionContent = "正确"
                                    };
                                    wxOptions.Add(wxOptionsInfo);
                                    WxOptionsInfo wxOptionsInfo2 = new WxOptionsInfo()
                                    {
                                        OptionTitle = "B",
                                        OptionContent = "错误"
                                    };
                                    wxOptions.Add(wxOptionsInfo2);
                                    options = JsonConvert.SerializeObject(wxOptions);
                                    answer = answerAndAnalysisEntity.answer.ToString() == "true" ? "A" : "B";
                                    answer = answer.ToUpper();
                                    break;
                                case 2://单选
                                    QuestionOption wxOptionsInfos = JsonConvert.DeserializeObject<QuestionOption>(questionInputs[i].Option);
                                    List<WxOptionsInfo> wxOptionsInfos1 = new List<WxOptionsInfo>();
                                    if (wxOptionsInfos != null)
                                    {
                                        if (!string.IsNullOrEmpty(wxOptionsInfos.A))
                                        {
                                            WxOptionsInfo wxOptionsInfoA = new WxOptionsInfo()
                                            {
                                                OptionTitle = "A",
                                                OptionContent = wxOptionsInfos.A,
                                            };
                                            wxOptionsInfos1.Add(wxOptionsInfoA);
                                        }
                                        if (!string.IsNullOrEmpty(wxOptionsInfos.B))
                                        {
                                            WxOptionsInfo wxOptionsInfoB = new WxOptionsInfo()
                                            {
                                                OptionTitle = "B",
                                                OptionContent = wxOptionsInfos.B,
                                            };
                                            wxOptionsInfos1.Add(wxOptionsInfoB);
                                        }
                                        if (!string.IsNullOrEmpty(wxOptionsInfos.C))
                                        {
                                            WxOptionsInfo wxOptionsInfoC = new WxOptionsInfo()
                                            {
                                                OptionTitle = "C",
                                                OptionContent = wxOptionsInfos.C,
                                            };
                                            wxOptionsInfos1.Add(wxOptionsInfoC);
                                        }
                                        if (!string.IsNullOrEmpty(wxOptionsInfos.D))
                                        {
                                            WxOptionsInfo wxOptionsInfoD = new WxOptionsInfo()
                                            {
                                                OptionTitle = "D",
                                                OptionContent = wxOptionsInfos.D,
                                            };
                                            wxOptionsInfos1.Add(wxOptionsInfoD);
                                        }
                                        if (!string.IsNullOrEmpty(wxOptionsInfos.E))
                                        {
                                            WxOptionsInfo wxOptionsInfoE = new WxOptionsInfo()
                                            {
                                                OptionTitle = "E",
                                                OptionContent = wxOptionsInfos.E,
                                            };
                                            wxOptionsInfos1.Add(wxOptionsInfoE);
                                        }
                                        if (!string.IsNullOrEmpty(wxOptionsInfos.F))
                                        {
                                            WxOptionsInfo wxOptionsInfoF = new WxOptionsInfo()
                                            {
                                                OptionTitle = "F",
                                                OptionContent = wxOptionsInfos.F,
                                            };
                                            wxOptionsInfos1.Add(wxOptionsInfoF);
                                        }
                                        if (!string.IsNullOrEmpty(wxOptionsInfos.G))
                                        {
                                            WxOptionsInfo wxOptionsInfoG = new WxOptionsInfo()
                                            {
                                                OptionTitle = "G",
                                                OptionContent = wxOptionsInfos.G,
                                            };
                                            wxOptionsInfos1.Add(wxOptionsInfoG);
                                        }
                                        options = JsonConvert.SerializeObject(wxOptionsInfos1);
                                        answer = string.Join(",", JsonConvert.DeserializeObject<List<string>>(answerAndAnalysisEntity.answer.ToString()));
                                        answer = answer.ToUpper();
                                    }
                                    break;
                                case 3://多选

                                    break;


                                default:
                                    break;
                            }
                            QuestionInput input = new QuestionInput();
                            input.Answer = answer;
                            input.QuestionType = questionType;
                            Regex regex = new Regex("(.*?)。");
                            MatchCollection matches = regex.Matches(questionInputs[i].Content);
                            if (matches.Count > 0)
                            {
                                if (matches.Count > 1)
                                {
                                    //Console.WriteLine($"序号{i + 2}  题目内容：{questionInputs[i].Content}    题型：{questionType}");
                                    ////string realContent = string.Empty;
                                    //foreach (var item in matches)
                                    //{
                                    //    realContent += item;
                                    //}
                                    //QuestionOutDataV1 questionImportData = new QuestionOutDataV1()
                                    //{
                                    //    Content = matches[0].Value,
                                    //    RealContent = realContent,
                                    //    QuestionType = questionType.ToString(),
                                    //    WorkType = fileName
                                    //};
                                    //questionImportDatas.Add(questionImportData);
                                }
                                input.Contents = matches[0].Value;
                                string realContent = string.Empty;
                                foreach (var item in matches)
                                {
                                    realContent += item;
                                }
                                input.Contents = realContent;
                            }
                            else
                            {
                                input.Contents = questionInputs[i].Content;
                            }
                            input.Explain = explain;
                            input.Options = options;
                            questionInputs1.Add(input);
                        }
                        catch (Exception ex)
                        {

                            throw;
                        }
                    }






                }

            }

            using (var strem = File.Create("output.xlsx"))
            {
                strem.SaveAs(questionInputs1);
            }

        }
    }


    public class Training
    {
        public int? TrainingType { get; set; }  
    }
    public class QuestionInput
    {
        #region 实体成员
        public long Id { get; set; }
        /// <summary>
        /// 准操项目
        /// </summary>	
        public string WorkType { get; set; }
        /// <summary>
        /// 题目类型 1判断题  2单选题  3多选题 
        /// </summary>		
        public int QuestionType { get; set; }
        /// <summary>
        /// 题干内容
        /// </summary>		
        public string Contents { get; set; }
        /// <summary>
        /// 问题答案
        /// </summary>		

        public string Answer { get; set; }
        /// <summary>
        /// 选项  JSON格式
        /// </summary>		
        public string Options { get; set; }
        /// <summary>
        /// 题目解析-知识点 
        /// </summary> 
        public string Explain { get; set; }

        /// <summary>
        /// 启用禁用  1：启用 0：禁用
        /// </summary>	
        public int EnabledMark { get; set; } = 1;

        public int IsInner { get; set; } = 0;

        #endregion
    }
    public class QuestionImportData
    {
        [ExcelColumnName("序号")]
        public int? Number { get; set; }
        [ExcelColumnName("类型-1单选3判断")]
        public string QuestionType { get; set; }
        [ExcelColumnName("题目")]
        public string Content { get; set; }
        [ExcelColumnName("选项")]
        public string Option { get; set; }
        [ExcelColumnName("答案和解析")]
        public string AnswerAndExplain { get; set; }

        /// <summary>
        /// 真正的内容
        /// </summary>
        public string RealContent { get; set; }

    }
    public class WxOptionsInfo
    {
        /// <summary>
        ///  A B C D
        /// </summary>
        public string OptionTitle { get; set; }
        /// <summary>
        /// 考试内容
        /// </summary>
        public string OptionContent { get; set; }

    }

    public class QuestionOption
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public string D { get; set; }
        public string E { get; set; }
        public string F { get; set; }
        public string G { get; set; }
    }

    public class AnswerAndAnalysisEntity
    {
        /// <summary>
        /// 
        /// </summary>
        public object answer { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string analysis { get; set; }
    }


    public class QuestionOutDataV1
    {

        [ExcelColumnName("题目类型")]
        public string QuestionType { get; set; }
        [ExcelColumnName("题目")]
        public string Content { get; set; }

        /// <summary>
        /// 真正的内容
        /// </summary>
        [ExcelColumnName("真实的题目")]
        public string RealContent { get; set; }

        /// <summary>
        /// 真正的内容
        /// </summary>
        [ExcelColumnName("工种")]
        public string WorkType { get; set; }

    }



}
